import { getUserInfoApi } from "@/apis/user";
import { useStore } from "@/store";

import { useRequest } from "ahooks";
import { useEffect, useState } from "react";
import useGetUserInfo from "./useGetUserInfo";




const useLoadUserData = ()=>{
  const token = useStore().token
  const [waitingUserData, setWaitingUserData] = useState(true)
    const store = useStore()

  const {run} = useRequest(getUserInfoApi,{
    manual: true,
    
    
    onSuccess: (data: any) => {
      store.setInfo(data)
    },

    onFinally:()=>{
      setWaitingUserData(false)
    }
  })

  //判断是否存在用户信息
  const {username} = useGetUserInfo()
  useEffect(()=>{
    if(username){
      setWaitingUserData(false)
      return 
    }
    if(token)
      run()
  // eslint-disable-next-line react-hooks/exhaustive-deps
  },[token])

  return {waitingUserData }
}



export default useLoadUserData;